package com.lm.paymentweb.DB;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.Map;

@Configuration
public class MysqlDataSourceConfig {

    @Bean
    @ConfigurationProperties("spring.datasource.mysql")
    @Primary
    public DataSourceProperties mysqlDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "dataSource")
    @Primary
    public HikariDataSource mysqlDataSource(HikariProperties hikariProperties) {
        HikariDataSource dataSource = mysqlDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
        for (Map.Entry<String, Object> entry : hikariProperties.getDataSourceProperties().entrySet()) {
            dataSource.addDataSourceProperty(entry.getKey(), entry.getValue());
        }

        return dataSource;
    }

    @Bean(name = "jdbcTemplate")
    @Primary
    public JdbcTemplate jdbcTemplate(HikariProperties hikariProperties) {
        HikariDataSource ds = mysqlDataSource(hikariProperties);
        return new JdbcTemplate(ds);
    }
}
